import os

from selenium.webdriver import ActionChains
from hyrobot.common import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from cfg.cfg_local_mysql import *
from lib.webui_local_mysql import *

class c73253b:
    name = '将超级管理员的申请记录读入文件并比较-c73253b'  # 测试用例名字

    def setup(self):
        STEP(1, '登陆网站')
        wd = open_browser()
        super_login(wd)
    # 清除方法
    def teardown(self):
        wd = get_global_webdriver()
        wd.quit()
    # 测试用例步骤
    def teststeps(self):
        # username = "sunxidong"
        # password = '123456'
        # 下面写登录流程
        driver = get_global_webdriver()

        # driver.find_element(By.NAME, "username").send_keys(username)
        # driver.find_element(By.NAME, "password").send_keys(password)
        # driver.find_element(By.CSS_SELECTOR, ".el-button").click()
        time.sleep(2)
        extending_button = driver.find_element_by_id("hamburger-container")
        try:
            driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            print("左侧栏没有隐藏")

        STEP(2, "进入产品管理界面")
        time.sleep(1)
        product_manage_word2 = driver.find_element(By.XPATH,
                                                   "/html/body/div/div/div[1]/ul/div[3]/a/li/span"
                                                   )
        product_manage_word2.click()
        time.sleep(1)
        top_title2 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第3个标题对应", "产品管理" == top_title2)

        STEP(3,"点击查看chanpin")
        time.sleep(1)
        product_input_box = driver.find_element(By.CSS_SELECTOR,
                                                "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)"
                                                )
        product_input_box.clear()
        time.sleep(1)
        product_input_box.send_keys("chanpin")
        # click select
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-form-item:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        # click inner check

        driver.find_element(By.CSS_SELECTOR,
                            "tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(1)"
                            ).click()
        time.sleep(1)
        STEP(4,"open file")
        # 每次使用前清空文档
        file_location = get_upload_directory() + '\\' + 'superuser_output_file.txt'
        superuser_output_file = open(file_location ,'w')
        # 包括汉语，共x条
        time.sleep(1)
        INFO('3')
        apply_num = driver.find_element(By.CSS_SELECTOR,
                            ".el-pagination__total"
                            ).text
        INFO(apply_num)
        apply_num_str = json.dumps(apply_num)
        superuser_output_file.write(apply_num_str + '\n')
        time.sleep(1)
        check_detail_btn = driver.find_elements(By.CLASS_NAME,"el-button")
        first_page_row_num = sum_row_num(driver)
        time.sleep(1)
        write_detail_into_file(driver,check_detail_btn,first_page_row_num,superuser_output_file)

        # clickable = driver.find_element(By.CSS_SELECTOR, ".el-pagination__editor > input:nth-child(1)") \
        #     .get_attribute('max')

        prev_button = driver.find_element_by_class_name("btn-next")
        if not prev_button.get_attribute("disabled"):
            INFO(prev_button.get_attribute("disabled"))
            while prev_button.get_attribute("disabled") == None:
                prev_button.click()
                next_row_len = sum_row_num(driver)
                time.sleep(1)
                next_page_check_btn = driver.find_elements(By.CLASS_NAME, "el-button")
                time.sleep(2)
                write_detail_into_file(driver, next_page_check_btn, next_row_len, superuser_output_file)

                time.sleep(1)
                prev_button = driver.find_element_by_class_name("btn-next")
                if prev_button.get_attribute("disabled") == "disabled":
                    break

        superuser_output_file.close()
        # 需要关闭之后，循环读入文件
        STEP(5,"比较两个文件里的内容")
        upload_location = get_upload_directory() + '\\' + 'productor_output_file.txt'
        super_location = get_upload_directory() + '\\' + 'superuser_output_file.txt'
        productor_read_file = open(upload_location,'r')
        superuser_read_file = open(super_location ,'r')
        line_num = 1
        for productor_read_line,superuser_read_line in zip(productor_read_file,superuser_read_file):
            INFO("this is line"+str(line_num))
            # only one parameter,str
            line_num += 1
            try:
                CHECK_POINT("比较此行的内容是否一致",productor_read_line == superuser_read_line)
            except:
                print('the',line_num,'wrong')

        productor_read_file.close()
        superuser_read_file.close()
        os.remove(upload_location)
        os.remove(super_location)

        # driver.quit()
